<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2012 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

// {{{ requires
require_once CLASS_EX_REALDIR . 'page_extends/LC_Page_Ex.php';
require_once CLASS_REALDIR . 'fckeditor/fckeditor.php';
/**
 * サイト概要のページクラス.
 *
 * @package Page
 * @author LOCKON CO.,LTD.
 * @version $Id:LC_Page_Abouts.php 15532 2007-08-31 14:39:46Z nanasess $
 */
class LC_Page_newsAdmin extends LC_Page_Ex {

    // }}}
    // {{{ functions

    /**
     * Page を初期化する.
     *
     * @return void
     */
    function init() {
        parent::init();
    }

    /**
     * Page のプロセス.
     *
     * @return void
     */
    function process() {
        parent::process();
        $this->action();
        $this->sendResponse();
    }

    /**
     * Page のアクション.
     *
     * @return void
     */
    function action() {
     switch ($this->getMode()) {
     	
            case 'confirm':
            	$this->adminAuthorityCheck(AUTHORITY_NEWS_ADMIN);
            	$this->tpl_title = 'News Confirm';
            	$this->tpl_mainpage = 'newsAdmin/newsPreview.tpl';    
            	$this->title = $_POST['title'];
            	$this->publishDate = $_POST['publishDate'];
            	$this->publisher = $_POST['publisher'];    
            	$this->newsContents = $this->getNewsContentsHtml();
            	$this->buttonFlg = "true";
            	$this->id = $this->getValFromPostOrGet("id");
                break;
            case 'showNews':
            	$this->tpl_title = 'News Confirm';
            	$this->tpl_mainpage = 'newsAdmin/newsPreview.tpl';    
            	$this->getNewsInfoFromDB();
                break;
                
            case 'insertToDB':
            	$this->adminAuthorityCheck(AUTHORITY_NEWS_ADMIN);
            	$id = $this->getValFromPostOrGet("id");
            	if(empty($id)){
            		$this->insertNewsToDB();
            	}else{
            		$this->updatenews($id);
            	}
            	SC_Response_Ex::sendRedirect(HTTP_URL."index.php");
                break;
                
            case 'editNews':
            	$this->adminAuthorityCheck(AUTHORITY_NEWS_ADMIN);      
        		$this->tpl_title = 'News Admin';
        		$this->tpl_mainpage = 'newsAdmin/newsPublish.tpl';  
        		
		        $this->linkCss[] = 'css/datepickerImport.css';
		        $this->link_javascript[] = "js/sapa/jquery.ui.core.js";
		        $this->link_javascript[] = "js/sapa/jquery.ui.datepicker.js";
		        $this->link_javascript[] = "js/sapa/jquery.ui.widget.js";
		        
		       
		        $this->getNewsInfoFromDB();
		        $this->initFCKeditor();
		        $this->id = $this->getValFromPostOrGet("id");
                break;
                
            case 'deleteNews':
            	$this->adminAuthorityCheck(AUTHORITY_NEWS_ADMIN);
            	$this->deleteNews();
            	SC_Response_Ex::sendRedirect(HTTP_URL."newsAdmin/index.php?mode=showNewslist");
                break;
            case 'showNewslist':
            	$this->adminAuthorityCheck(AUTHORITY_NEWS_ADMIN);
            	$this->tpl_title = 'News Confirm';
            	$this->tpl_mainpage = 'newsAdmin/news_list.tpl'; 
            	$this->linkCss[] = 'css/sapaImport.css';
				//引入本页面javascript
				$this->link_javascript[] = "js/sapa/jquery.dataTables.js";
            	$this->getNewsInfoList();
                break;
            default:
            	$this->adminAuthorityCheck(AUTHORITY_NEWS_ADMIN);      
        		$this->tpl_title = 'News Admin';
        		$this->tpl_mainpage = 'newsAdmin/newsPublish.tpl';  
        		
		        $this->linkCss[] = 'css/datepickerImport.css';
		        $this->link_javascript[] = "js/sapa/jquery.ui.core.js";
		        $this->link_javascript[] = "js/sapa/jquery.ui.datepicker.js";
		        $this->link_javascript[] = "js/sapa/jquery.ui.widget.js";
		        
		        $this->initFCKeditor();
                break;
        }


    }

    /**
     * デストラクタ.
     *
     * @return void
     */
    function destroy() {
        parent::destroy();
    }
    
    function initFCKeditor(){
		$fckeditor = new FCKeditor('newsContents');
		$fckeditor->BasePath = HTTP_URL."fckeditor/";
		
		if($this->getMode() == 'editNews'){
			$fckeditor->Value = $this->newsContents;
		}else{
			$fckeditor->Value ="please write here";
		}
		
		$fckeditor->Height ="350px";
		//$fckeditor->Config['DefaultLanguage']='eo';
		$this->fckeditor = $fckeditor->CreateHtml();
		
	}
   
	function getNewsContents(){
		//$pattern = '/^[a-zA-Z0-9_]+$/';
		$newsContents=null;
		//if(isset($_POST['newsContents']) && preg_match($pattern, $_POST['newsContents'])) {
		// $newsContents = $_POST['newsContents'];
		//}
		if ( isset( $_POST ) )
		$this->postArray = &$_POST ;			// 4.1.0 or later, use $_POST
		else
		$this->postArray = &$HTTP_POST_VARS ;	// prior to 4.1.0, use HTTP_POST_VARS

		foreach ( $this->postArray as $sForm => $value )
		{
			if ( get_magic_quotes_gpc() )
			$newsContents = htmlspecialchars( stripslashes( $value ) ) ;
			else
			$newsContents = htmlspecialchars( $value ) ;
		}

		$newsContents = htmlspecialchars( $this->postArray['newsContents'] ) ;

		return $newsContents;
			
	}
	
	
	
	function getNewsContentsHtml(){
		$newsContents=null;
		if ( isset( $_POST ) )
		$this->postArray = &$_POST ;			// 4.1.0 or later, use $_POST
		else
		$this->postArray = &$HTTP_POST_VARS ;	// prior to 4.1.0, use HTTP_POST_VARS

		return $this->postArray['newsContents'];
		 
	}
	
	function insertNewsToDB() {
		$title = $_POST['title'];
        $publishDate = $_POST['publishDate'];
        $publisher = $_POST['publisher'];    
        $newsContents = $this->getNewsContentsHtml();
        
        $insertArr = array();
        
        $insertArr['title'] = $title;
        $insertArr['publish_date'] = $publishDate;
        $insertArr['publisher'] = $publisher;
        $insertArr['news_contents'] = $newsContents;
        $insertArr['del_flg'] = '0';
        $insertArr['create_time'] = 'now()';
        $insertArr['update_time'] = 'now()';
        
        $objQuery = SC_Query::getSingletonInstance();
        $objQuery->begin();
        $objQuery->insert('mst_news', $insertArr);
        
		//插入数据库时 出错了	
		if ($objQuery->isError()) {
			$objQuery->rollback();
			SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'system error please contact the administrator.');
			break;
		}else{
			$objQuery->commit();
		}
            	
	}
	function getNewsInfoFromDB() {
		$newsId = $this->getValFromPostOrGet("id");
		if (!empty($newsId)) {
			 $objQuery = SC_Query::getSingletonInstance();
        	 $newsInfo = $objQuery->select("*",'mst_news', "id=?",array($newsId));
        	 
        	 
        	 $this->title =$newsInfo[0]['title'];
             $this->publishDate = $newsInfo[0]['publish_date'];
             $this->publisher = $newsInfo[0]['publisher'];    
             $this->newsContents = $newsInfo[0]['news_contents'];
		}
		
	}
	
	function getNewsInfoList() {
			$objQuery = SC_Query::getSingletonInstance();
        	$this->newsInfolist = $objQuery->select("*",'mst_news');
        	
        	foreach ($this->newsInfolist as $key => $value) {
        		if($value['del_flg'] == "0"){
        			$this->newsInfolist[$key]['status'] = "NorMal";
        		}
        		if($value['del_flg'] == "1"){
        			$this->newsInfolist[$key]['status'] = "Blocked";
        		}
        	}
	}
	
	function deleteNews() {
		$newsId = $this->getValFromPostOrGet("id");
		if (!empty($newsId)) {
			 $objQuery = SC_Query::getSingletonInstance();
			 $value = array();
			 $value['del_flg'] = '1';
        	 $objQuery->update('mst_news', $value,"id= ?",array($newsId));
        	 
		}
	}
	
	function updateNews($id) {
		$title = $_POST['title'];
        $publishDate = $_POST['publishDate'];
        $publisher = $_POST['publisher'];    
        $newsContents = $this->getNewsContentsHtml();
        
        $updateArr = array();
        $updateArr['title'] = $title;
        $updateArr['publish_date'] = $publishDate;
        $updateArr['publisher'] = $publisher;
        $updateArr['news_contents'] = $newsContents;
        $updateArr['del_flg'] = '0';
        
        $objQuery = SC_Query::getSingletonInstance();
        $objQuery->begin();
        $objQuery->update('mst_news', $updateArr,"id= ? " ,array($id));
        
		//插入数据库时 出错了	
		if ($objQuery->isError()) {
			$objQuery->rollback();
			SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'system error please contact the administrator.');
			break;
		}else{
			$objQuery->commit();
		}
            	
	}
	
}
